package com.maple.design.aop.jdk;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * <p></p>
 *
 * @author maple
 * @since 2023/3/28
 */
public class JdkDemoProxy implements InvocationHandler {

    // 被代理的目标对象
    private Object proxyObj;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("jdk start...");
        Object object = method.invoke(proxyObj,args);
        System.out.println("jdk end...");
        return object;
    }

    private Object getProxyInstance(Object obj) {
        this.proxyObj = obj;
        return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
    }

    public static void main(String[] args) {
        JdkDemoProxy jdkDemoProxy = new JdkDemoProxy();
        JdkDemoService jdkDemoService = (JdkDemoService) jdkDemoProxy.getProxyInstance(new JdkDemoServiceImpl());
        jdkDemoService.show("maple second");
    }
}
